<?php
include_once("game.php");
class GameRepository
{

function createGame($matchID, $gameNumber, $player1Score, $player2Score){
	$dbConnection = mysql_connect ('127.0.0.1', 'root', 'root')
		or die ();
	@mysql_select_db ('rapidfireleague' ,$dbConnection)
		or die ();
	$insertQuery=sprintf("INSERT INTO `game` (`matchID`, `gameNumber`, `player1Score`, `player2Score`) VALUES(%d,%d,%d,%d)",$matchID, $gameNumber, $player1Score, $player2Score);
	mysql_query($insertQuery,$dbConnection);	
	mysql_free_result($insertQuery);
	mysql_close($dbConnection);	
}

function readGame($matchID, $gameNumber){
	$dbConnection = mysql_connect ('127.0.0.1', 'root', 'root')
		or die ();
	@mysql_select_db ('rapidfireleague' ,$dbConnection)
		or die ();
	$selectQuery = sprintf("Select * from `game` where `matchID` = %d and `gameNumber` = %d", $matchID, $gameNumber);
	$game=mysql_query($selectQuery,$dbConnection);
	mysql_free_result($selectQuery);
	mysql_close($dbConnection);	
	return $game;
}

function updateGame($matchID, $gameNumber, $player1Score, $player2Score){
	$dbConnection = mysql_connect ('127.0.0.1', 'root', 'root')
		or die ();
	@mysql_select_db ('rapidfireleague' ,$dbConnection)
		or die ();
	$updateQuery = sprintf("Update `game` set `player1Score`=%d, `player2Score`=%d where `matchID` = %d and `gameNumber`=%d",$player1Score, $player2Score, $matchID, $gameNumber);
	mysql_query($updateQuery,$dbConnection);
	mysql_free_result($updateQuery);
	mysql_close($dbConnection);	
}

function deleteGame($matchID, $gameNumber){
	$dbConnection = mysql_connect ('127.0.0.1', 'root', 'root')
		or die ();
	@mysql_select_db ('rapidfireleague' ,$dbConnection)
		or die ();
	$deleteQuery = sprintf("delete from `game` where `matchID` = %d and `gameNumber`=%d", $matchID, $gameNumber);
	mysql_query($deleteQuery,$dbConnection);
	mysql_free_result($deleteQuery);
	mysql_close($dbConnection);	
}

function map($row){
	$game= new Game;
	while($arrRecords = mysql_fetch_array($row)){
	$game->matchID=$arrRecords['matchID'];
	$game->gameNumber=$arrRecords['gameNumber'];
	$game->player1Score=$arrRecords['player1Score'];
	$game->player2Score=$arrRecords['player2Score'];
	}
	return $game;
}
function maps($rows){
	$games = array();
	while($arrRecords = mysql_fetch_array($rows)){
	$game= new Game;
	$game->matchID=$arrRecords['matchID'];
	$game->gameNumber=$arrRecords['gameNumber'];
	$game->player1Score=$arrRecords['player1Score'];
	$game->player2Score=$arrRecords['player2Score'];
	$games[]=$game;
	}
	return $games;
}

function getGamesByMatchID($matchID){
	$dbConnection = mysql_connect ('127.0.0.1', 'root', 'root')
		or die ();
	@mysql_select_db ('rapidfireleague' ,$dbConnection)
		or die ();
	$selectQuery = sprintf("Select * from `game` where `matchID` = %d", $matchID);
	$games=mysql_query($selectQuery,$dbConnection);	
	mysql_free_result($selectQuery);
	mysql_close($dbConnection);	
	return $games;

}

function checkGame($matchID, $gameNumber){
	$dbConnection = mysql_connect ('127.0.0.1', 'root', 'root')
		or die ();
	@mysql_select_db ('rapidfireleague' ,$dbConnection)
		or die ();
	$selectQuery = sprintf("Select * from `game` where `matchID` = %d and `gameNumber` = %d", $matchID, $gameNumber);
	$result=mysql_query($selectQuery,$dbConnection);
	$num_rows = mysql_num_rows($result);
	mysql_free_result($selectQuery);
	mysql_close($dbConnection);	
	
	return $num_rows;
	
}


}
?>